// DefinitionQueryHandler.cpp: implementation of the DefinitionQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "DefinitionQueryHandler.h"
#include "CoalMineUnit.h"
#include "Server.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"
#include "Connection.h"
#include "ConnectionManager.h"

#include "CommServerLib\CommWithClient.h"

#include "MonitoringUnitLib\BaseMonitoringUnit.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

DefinitionQueryHandler::DefinitionQueryHandler(const Packet& packet, HANDLE hClient, bool bIsRequestEx):
	BaseRequestHandler(packet, hClient)
{
	m_bIsRequestEx = bIsRequestEx;
}

DefinitionQueryHandler::DefinitionQueryHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

DefinitionQueryHandler::~DefinitionQueryHandler()
{
}

bool DefinitionQueryHandler::Prehandling()
{
	DefinitionQueryStruct * pDefinitionQueryStruct = (DefinitionQueryStruct *)m_pPacket->GetpData();

	DefinitionAckStruct * pDefinitionAckStruct = NULL;

	int nsize = SIZE_COALMINENAME;
	if (pDefinitionQueryStruct->m_nCoalMineId == 0)
	{
		if (pDefinitionQueryStruct->m_dwNumOfItems > 0)
		{
			pDefinitionAckStruct = (DefinitionAckStruct *)new BYTE[sizeof(DefinitionAckStruct)+sizeof(MonUnitDefItem)*(pDefinitionQueryStruct->m_dwNumOfItems-1)];

			for (int i = 0;i < pDefinitionQueryStruct->m_dwNumOfItems;i++)
			{
				MonUnitItem * pMonUnitItem = &pDefinitionQueryStruct->m_MonUnitItems[i];
				MonUnitDefItem * pMonUnitDefItem = &pDefinitionAckStruct->m_DefItems[i];

				pMonUnitDefItem->m_nCoalMineId = pMonUnitItem->m_nCoalMineId;

				bool bSuccess = false;
				CoalMineUnit * pCoalMineUnit = (CoalMineUnit *)Server::Instance()->GetCoalMineUnitById(pMonUnitItem->m_nCoalMineId);
				if (pCoalMineUnit)
				{
					ASSERT(sizeof(pMonUnitDefItem->m_szCoalMineName) > pCoalMineUnit->GetName().GetLength());
					strcpy(pMonUnitDefItem->m_szCoalMineName, pCoalMineUnit->GetName());
					BaseMonitoringUnit * pMonitoringUnit = pCoalMineUnit->GetMonitoringUnitById(pMonUnitItem->m_szMonUnitId);
					if (pMonitoringUnit)
					{
						pMonitoringUnit->ConvertDefToStruct(&pMonUnitDefItem->m_MonUnitDef);
						bSuccess = true;
					}
				}

				pMonUnitDefItem->m_bSuccess = bSuccess;
			}
		}
		else
		{
			pDefinitionAckStruct = new DefinitionAckStruct;
			pDefinitionAckStruct->m_dwBufSize = sizeof(DefinitionAckStruct);
			pDefinitionAckStruct->m_dwNumOfItems = 0;
		}
	}
	else
	{
		CoalMineUnit * pCoalMineUnit = (CoalMineUnit *)Server::Instance()->GetCoalMineUnitById(pDefinitionQueryStruct->m_nCoalMineId);
		MyConnection * pConnection = ConnectionManager::Instance()->GetConnectionByHandle(m_hClient);
		int nId = pDefinitionQueryStruct->m_nCoalMineId;
		if(pCoalMineUnit && pConnection->m_nIdList[nId]==nId ){
			const std::vector<BaseMonitoringUnit *>& MonitoringUnits = pCoalMineUnit->GetMonitoringUnits();
			if (MonitoringUnits.size() > 0)
			{
				DWORD dwBufSize = sizeof(DefinitionAckStruct)+sizeof(MonUnitDefItem)*(MonitoringUnits.size()-1);
				pDefinitionAckStruct = (DefinitionAckStruct *)new BYTE[dwBufSize];

				for (int i = 0;i < MonitoringUnits.size();i++)
				{
					BaseMonitoringUnit * pMonitoringUnit = MonitoringUnits[i];
					MonUnitDefItem * pMonUnitDefItem = &pDefinitionAckStruct->m_DefItems[i];
					pMonUnitDefItem->m_nCoalMineId = pDefinitionQueryStruct->m_nCoalMineId;
					//2005-8-7
					//ASSERT(sizeof(pMonUnitDefItem->m_szCoalMineName) > pCoalMineUnit->GetName().GetLength());
					strncpy(pMonUnitDefItem->m_szCoalMineName, pCoalMineUnit->GetName(),sizeof(pMonUnitDefItem->m_szCoalMineName));
					pMonitoringUnit->ConvertDefToStruct(&pMonUnitDefItem->m_MonUnitDef);
					pMonUnitDefItem->m_bSuccess = true;
				}
				pDefinitionAckStruct->m_dwBufSize = dwBufSize;
				pDefinitionAckStruct->m_nCoalMineId = pDefinitionQueryStruct->m_nCoalMineId;
				pDefinitionAckStruct->m_dwNumOfItems = MonitoringUnits.size();
			}
		}
		if(!pDefinitionAckStruct)
		{
			pDefinitionAckStruct = new DefinitionAckStruct;
			pDefinitionAckStruct->m_dwBufSize = sizeof(DefinitionAckStruct);
			pDefinitionAckStruct->m_nCoalMineId = pDefinitionQueryStruct->m_nCoalMineId;
			pDefinitionAckStruct->m_dwNumOfItems = 0;
		}
	}

	pDefinitionAckStruct->m_dwMethodType = VCS_MT_QUERYDATA;
	pDefinitionAckStruct->m_nMethodAck = VCS_MR_OK;

	Packet packetAck(pDefinitionAckStruct->m_dwBufSize, (BYTE *)pDefinitionAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pDefinitionAckStruct;

	return true;
}

bool DefinitionQueryHandler::PrehandlingEx()
{
	DefinitionQueryStructEx * pDefinitionQueryStruct = (DefinitionQueryStructEx *)m_pPacket->GetpData();

	DefinitionAckStructEx * pDefinitionAckStruct = NULL;

	int nsize = SIZE_COALMINENAME;
	if (pDefinitionQueryStruct->m_nCoalMineId == 0)
	{
		if (pDefinitionQueryStruct->m_dwNumOfItems > 0)
		{
			pDefinitionAckStruct = (DefinitionAckStructEx *)new BYTE[sizeof(DefinitionAckStructEx)+sizeof(MonUnitDefItemEx)*(pDefinitionQueryStruct->m_dwNumOfItems-1)];

			for (int i = 0;i < pDefinitionQueryStruct->m_dwNumOfItems;i++)
			{
				MonUnitItemEx * pMonUnitItem = &pDefinitionQueryStruct->m_MonUnitItems[i];
				MonUnitDefItemEx * pMonUnitDefItem = &pDefinitionAckStruct->m_DefItems[i];

				pMonUnitDefItem->m_nCoalMineId = pMonUnitItem->m_nCoalMineId;

				bool bSuccess = false;
				CoalMineUnit * pCoalMineUnit = (CoalMineUnit *)Server::Instance()->GetCoalMineUnitById(pMonUnitItem->m_nCoalMineId);
				if (pCoalMineUnit)
				{
					ASSERT(sizeof(pMonUnitDefItem->m_szCoalMineName) > pCoalMineUnit->GetName().GetLength());
					strcpy(pMonUnitDefItem->m_szCoalMineName, pCoalMineUnit->GetName());
					BaseMonitoringUnit * pMonitoringUnit = pCoalMineUnit->GetMonitoringUnitById(pMonUnitItem->m_szMonUnitId);
					if (pMonitoringUnit)
					{
						pMonitoringUnit->ConvertDefToStruct(&pMonUnitDefItem->m_MonUnitDef);
						bSuccess = true;
					}
				}

				pMonUnitDefItem->m_bSuccess = bSuccess;
			}
		}
		else
		{
			pDefinitionAckStruct = new DefinitionAckStructEx;
			pDefinitionAckStruct->m_dwBufSize = sizeof(DefinitionAckStructEx);
			pDefinitionAckStruct->m_dwNumOfItems = 0;
		}
	}
	else
	{
		CoalMineUnit * pCoalMineUnit = (CoalMineUnit *)Server::Instance()->GetCoalMineUnitById(pDefinitionQueryStruct->m_nCoalMineId);
		MyConnection * pConnection = ConnectionManager::Instance()->GetConnectionByHandle(m_hClient);
		int nId = pDefinitionQueryStruct->m_nCoalMineId;
		if(pCoalMineUnit && pConnection->m_nIdList[nId]==nId ){
			const std::vector<BaseMonitoringUnit *>& MonitoringUnits = pCoalMineUnit->GetMonitoringUnits();
			if (MonitoringUnits.size() > 0)
			{
				DWORD dwBufSize = sizeof(DefinitionAckStructEx)+sizeof(MonUnitDefItemEx)*(MonitoringUnits.size()-1);
				pDefinitionAckStruct = (DefinitionAckStructEx *)new BYTE[dwBufSize];

				for (int i = 0;i < MonitoringUnits.size();i++)
				{
					BaseMonitoringUnit * pMonitoringUnit = MonitoringUnits[i];
					MonUnitDefItemEx * pMonUnitDefItem = &pDefinitionAckStruct->m_DefItems[i];
					pMonUnitDefItem->m_nCoalMineId = pDefinitionQueryStruct->m_nCoalMineId;
					//2005-8-7
					//ASSERT(sizeof(pMonUnitDefItem->m_szCoalMineName) > pCoalMineUnit->GetName().GetLength());
					strncpy(pMonUnitDefItem->m_szCoalMineName, pCoalMineUnit->GetName(),sizeof(pMonUnitDefItem->m_szCoalMineName));
					pMonitoringUnit->ConvertDefToStruct(&pMonUnitDefItem->m_MonUnitDef);
					pMonUnitDefItem->m_bSuccess = true;
				}
				pDefinitionAckStruct->m_dwBufSize = dwBufSize;
				pDefinitionAckStruct->m_nCoalMineId = pDefinitionQueryStruct->m_nCoalMineId;
				pDefinitionAckStruct->m_dwNumOfItems = MonitoringUnits.size();
			}
		}
		if(!pDefinitionAckStruct)
		{
			pDefinitionAckStruct = new DefinitionAckStructEx;
			pDefinitionAckStruct->m_dwBufSize = sizeof(DefinitionAckStructEx);
			pDefinitionAckStruct->m_nCoalMineId = pDefinitionQueryStruct->m_nCoalMineId;
			pDefinitionAckStruct->m_dwNumOfItems = 0;
		}
	}

	pDefinitionAckStruct->m_dwMethodType = VCS_MT_QUERYDATA_EX;
	pDefinitionAckStruct->m_nMethodAck = VCS_MR_OK;

	Packet packetAck(pDefinitionAckStruct->m_dwBufSize, (BYTE *)pDefinitionAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pDefinitionAckStruct;

	return true;
}